package com.sdy.auth.client.service;

import com.sdy.auth.api.model.UserInfo;
import com.sdy.auth.client.config.SsoConfig;
import com.sdy.mvc.utils.HttpUtil;
import com.sdy.redis.service.RedisService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>File: SsoService.java</p>
 * <p>Description: SsoService</p>
 *
 * @author zzq
 * @version V1.0
 * @since 2019-05-09
 */
@Service
public class SsoService {
    @Autowired
    private SsoConfig ssoConfig;
    @Autowired
    private RedisService redisService;
    
    public UserInfo getUserInfo(HttpServletRequest request) {
        String token = HttpUtil.getUserToken(request);
        if (token == null) {
            return null;
        }
        return redisService.get(token, UserInfo.class);
    }

    public Integer getUserId(HttpServletRequest request) {
        UserInfo userInfo = getUserInfo(request);
        return userInfo == null ? null : userInfo.getUserId();
    }
    
    public void logout(HttpServletRequest request) {
        String token = (String) request.getAttribute("token");
        if (StringUtils.isNotBlank(token)) {
            redisService.del(token);
        }
    }
}
